package com.yunxi.controller.sys;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sys.pojo.OrgVO;
import com.yunxi.sys.service.OrgDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 组织结构 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-05-08
 */
@RestController
@RequestMapping("/admin/org")
@Api(tags = "sys:org", description = "组织结构接口")
public class OrgController {
    private static final Logger logger = LoggerFactory.getLogger(OrgController.class);

    /*@Reference(version = "${demo.service.version}",
            application = "${dubbo.application.id}",
            url = "${dubbo.application.url}",
            check = false,
            timeout = 1000000)*/
    @Autowired
    OrgDubboService orgDubboService;

    @GetMapping("/tree")
    @ApiOperation(value = "组织树形查询")
    public ApiResult tree(OrgVO orgVO) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Integer orgType = orgVO.getOrgType();
        String org_type = null;
        if (orgType != null) {
            org_type = orgType.toString();
        }
        return (ApiResult)orgDubboService.findTreeListBySysType(org_type);
    }

    @GetMapping("/treeByPid")
    @ApiOperation(value = "组织树形查询")
    public ApiResult treeByPid(String pid) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return (ApiResult)orgDubboService.treeByPid(pid);
    }

    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    @ApiImplicitParams(
            {@ApiImplicitParam(paramType = "query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
                    @ApiImplicitParam(paramType = "query", name = "pageSize", value = "分页数量", required = true, dataType = "int")})
    public ApiResult page(OrgVO orgVO, Integer pageNum, Integer pageSize) throws Exception {
        return ResponseUtil.ok(orgDubboService.findPageByVO(orgVO, pageNum, pageSize));
    }

    @GetMapping("/load")
    @ApiOperation(value = "读取单个")
    public ApiResult load(@RequestParam String orgId, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(orgDubboService.getById(orgId));
    }

    @GetMapping("/listByVO")
    @ApiOperation(value = "根据vo条件查询组织机构")
    public ApiResult listByVO(OrgVO orgVO) throws Exception {
        return ResponseUtil.ok(orgDubboService.listByVO(orgVO));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public ApiResult save(@RequestBody OrgVO orgVo, @CurrentUser LoginInfo loginInfo) throws Exception {
        orgDubboService.saveOrUpdate(orgVo);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public ApiResult del(@RequestBody List<OrgVO> orgVoList) throws IllegalAccessException, InstantiationException, InvocationTargetException {
        orgDubboService.removeByListVO(orgVoList);
        return ResponseUtil.ok("success");
    }

    @GetMapping("/getList")
    @ApiOperation(value = "读取单个")
    @ApiIgnore
    public ApiResult getList(OrgVO orgVo, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(orgDubboService.getList());
    }
/*@PostMapping("/export")
@ApiOperation(value = "导出excel")
public Object export(@RequestParam Map<String,Object> params) throws Exception {
return ResponseUtil.ok(orgDubboService.findMapByList(params));
}*/
}
